package com.hotel.project.demo.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.hotel.project.demo.domain.HotelEntity;
import com.hotel.project.demo.domain.ResponseBean;
import com.hotel.project.demo.service.HotelService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/hotel")
public class HotelController extends BaseController<HotelService, HotelEntity>{
    /**
     * 新增酒店
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public ResponseBean saveHotel(HotelEntity entity){
        ResponseBean rb = new ResponseBean();
        try {
            if (!service.save(entity)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }


    /**
     * 更新酒店基本信息
     * @param entity
     * @return
     */
    @PutMapping("/admin/edit-hotel")
    public ResponseBean updateHotelBaseMessage(HotelEntity entity){
        ResponseBean rb = new ResponseBean();
        try{
            if (!service.updateById(entity)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }

    /**
     * 更新酒店缓存
     * @param id
     * @param hotelName

     * @return
     */
    @PutMapping("/admin/cache/update")
    public ResponseBean updateHotelCache(@PathVariable Long id, @PathVariable String hotelName){
        ResponseBean rb = new ResponseBean();
        try {
            if (!service.updateHotelCache(id,hotelName)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;

    }

    /**
     * 更新酒店评分缓存
     * @param hotelName
     * @param evaluateScore
     * @return
     */
    @PostMapping("/evaluate/cache")
    public ResponseBean updateEvaluateCache(@PathVariable String hotelName,@PathVariable Integer evaluateScore){
        ResponseBean rb = new ResponseBean();
        try {
            if (!service.updateHotelEvaluateCache(hotelName, evaluateScore)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }

    /**
     * 更新酒店销量缓存
     * @param hotelName
     * @param saleCount
     * @return
     */
    @PostMapping("/sale/cache")
    public ResponseBean updateSaleCache(@PathVariable String hotelName,@PathVariable Integer saleCount){
        ResponseBean rb = new ResponseBean();
        try {
            if (!service.updateHotelSaleCache(hotelName, saleCount)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }
    /**
     * 更新酒店收藏缓存
     * @param hotelName
     * @return
     */
    @PostMapping("/collect/cache")
    public ResponseBean updateCollectCache(@PathVariable String hotelName,Integer collectCount){
        ResponseBean rb = new ResponseBean();
        try {
            if (!service.updateHotelCollectCache(hotelName,collectCount)){
                rb = errorEdit(rb,10001l);
            }
        }catch (Exception e){
            e.printStackTrace();
            rb = errorEdit(rb,10001l);
        }
        return rb;
    }

    @DeleteMapping("/admin/delete")
    public ResponseBean updateHotelStatus(@PathVariable Long id,@PathVariable String hotelName,@PathVariable Integer deleted){
        Integer temp;
        ResponseBean rb = new ResponseBean();
        temp = service.updateHotelStatus(id,hotelName,deleted);
        if (temp == 0){
            rb = errorEdit(rb,501l);
            rb.setMessage("酒店未找到");
        }
        if (temp == -1){
            rb = errorEdit(rb,502l);
            rb.setMessage("服务器错误");
        }
        return rb;
    }
}
